package priv.jesse.mall.web.user;


import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import priv.jesse.mall.entity.Product;
import priv.jesse.mall.service.ProductService;
import priv.jesse.mall.web.weixin.WXPay;
import priv.jesse.mall.web.weixin.WXPayConfig;
import priv.jesse.mall.web.weixin.WXPayConstants;
import priv.jesse.mall.web.weixin.WXPayUtil;

import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/weixinpay")
public class WeiXinPayController {
    @Autowired
    private ProductService productService;

    @Value("${appId:88888888888}")
    public String appId;

    @Value("${mchId:88888888888}")
    public String mchId;

    @Value("${key:88888888888}")
    public String key;

    /**
     *微信支付业务
     * @return
     */
    @RequestMapping("/weixinpayhandler.do")
    public Map<String, String> toProductPage(Integer orderid,String spbill_create_ip,Integer total_fee,Map<String, Object> map) {
        WXPayConfig wxPayConfig = new WXPayConfig();
        wxPayConfig.setAppID(appId);
        wxPayConfig.setMchID(mchId);
        wxPayConfig.setKey(key);
        try {
            WXPay wxPay = new WXPay(wxPayConfig);
            Map<String, String> reqData = new HashMap<>();
            reqData.put("body", "商品支付");
            reqData.put("out_trade_no", orderid.toString());
            reqData.put("total_fee", total_fee.toString());
            reqData.put("spbill_create_ip", spbill_create_ip);
            reqData.put("notify_url", "wwww.baidu.com");
            reqData.put("trade_type", "MWEB");
            JSONObject scene_info = new JSONObject();
            scene_info.put("h5_info", "h5_info");
            scene_info.put("type", "1");
            scene_info.put("wap_url", "wwww.mtjz.com");
            scene_info.put("wap_name", "茅台酒镇");
            reqData.put("scene_info", scene_info.toString());
            Map<String, String> paymap=wxPay.fillRequestData(reqData);
            Map<String, String> resultmap= wxPay.unifiedOrder(paymap);
            JSONObject resultjson=new JSONObject(resultmap);
            System.out.println(resultjson.toString());
            String return_code =resultmap.get("return_code ");
            String result_code =resultmap.get("result_code ");
            if("SUCCESS".equals(return_code) && "SUCCESS".equals(result_code)){
                String prepay_id =resultmap.get("prepay_id");
                String nonceStr =resultmap.get("nonce_str");
                Map<String, String> payData = new HashMap<>();
                payData.put("appId",appId);
                payData.put("timeStamp",System.currentTimeMillis()+"");
                payData.put("package","prepay_id="+prepay_id);
                payData.put("nonceStr",nonceStr);
                payData.put("signType","MD5");
                String paySign= WXPayUtil.generateSignature(reqData, key, WXPayConstants.SignType.MD5);
                payData.put("paySign",paySign);
                return payData;
            }else{
                return new HashMap<>();
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            return new HashMap<>();
        }
    }





    /**
     *微信支付通知
     * @return
     */
    @RequestMapping("/weixinpayReceive.do")
    public void weixinpayReceive() {
      System.out.println("接收微信支付结果");
    }




















}
